package com.sishu.websocket;

//import com.sishu.websocket.po.Users;

import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

import javax.annotation.Resource;

@Component
public class WebSocketAPI extends TextWebSocketHandler {

    @Resource
    private MyWebSocketServer myWebSocketServer;



    @Override //该方法会在websocket连接成功后被调用
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        myWebSocketServer.onOpen(session);
        System.out.println("建立连接了");
    }
    @Override //该方法是在websocket收到消息的时候自动调用
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        myWebSocketServer.onMessage(message, session);
    }

    @Override //该方法是在websocket连接出现异常的时候自动调用的
    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
        myWebSocketServer.onError(session,exception);
        System.out.println("连接出现异常了");
    }
    @Override //该方法是在websocket连接关闭后自动调用的
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        //status 为关闭的状态
        myWebSocketServer.onClose(session);
    }

}